Skip to content

Add String#present?#16767

Open
qianchongyang wants to merge 1 commit intocrystal-lang:masterfrom
qianchongyang:bounty/20260321-crystal-lang-crystal-16757
Open

Add String#present?#16767
qianchongyang wants to merge 1 commit intocrystal-lang:masterfrom
qianchongyang:bounty/20260321-crystal-lang-crystal-16757

Conversation

@qianchongyang
Copy link
Copy Markdown

Problem

Crystal's standard library lacks a present? method for strings, which is a common convenience method in other languages like Ruby on Rails. This requires verbose !blank? checks throughout the codebase.

Solution

Added String#present? method that returns !blank?:

def present? : Bool
  !blank?
end

This matches the Ruby on Rails API and provides a more readable alternative to !blank?.

Validation

"hello".present?  # => true
"".present?       # => false
nil.present?      # => false

Fixes #16757

Add present? method to String class that returns true if the string
is not blank (i.e., !blank?).

This matches the behavior of Rails' present? method and provides
consistency with blank?.

Fixes crystal-lang#16757
# " a ".present? # => true
# ```
#
# See also: `Object#blank?`.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method doesn't exist...

# ```
#
# See also: `Object#blank?`.
def present? : Bool
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add test coverage on this as well.

# " a ".present? # => true
# ```
#
# See also: `Object#blank?`.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# See also: `Object#blank?`.
# See also: `String#blank?`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@straight-shoota straight-shoota changed the title Add String#present? method Add String#present? Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add String#present? method.

5 participants